//
//  NewFeatureViewController.swift
//  WeiBlog
//
//  Created by apple on 15/10/19.
//  Copyright © 2015年 xiang. All rights reserved.
//

import UIKit
import SnapKit
private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {

    ///  指定构造函数(布局)
    ///
    ///  - returns: NewFeatureViewController
    init(){
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = CGSize(width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height)
        layout.scrollDirection = .Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:-------viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        
        //注册cell
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)


    }


    // MARK:------数据源UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return 4
    }

    ///  返回cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.imageView.image = UIImage(named: "new_feature_\(indexPath.item+1)")
        
        if indexPath.item != 3{
            
            cell.beginBtn.hidden = true
        }else
        {
            cell.beginBtn.hidden = false
            cell.startAnimation()
        }
        
        return cell
    }
}


private class NewFeatureCell: UICollectionViewCell {
    
// MARK:-------懒加载
    private lazy var imageView:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
 
    private lazy  var beginBtn:UIButton = UIButton(title: "开始体验",fontSize:nil, color: UIColor.whiteColor(), bgImageName: "tabbar_compose_button", imageName: nil, actionName: "click", target: self)
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
    }
    
    
// MARK:-------点击事件(通知事件)
    @objc private func click(){
        
        if UserAccountViewModel.sharedUserAccountViewModel.isLogin {
            //跳转到欢迎页面
            
            NSNotificationCenter.defaultCenter().postNotificationName("WBJumpToNotification", object: "welcome")
            
        }else{
            
            //跳转到主界面
            
            NSNotificationCenter.defaultCenter().postNotificationName("WBJumpToNotification", object: nil)
        }

    }
// MARK:-------设置UI
    private func setUI(){
        
        //添加控件
        contentView.addSubview(imageView)
        contentView.addSubview(beginBtn)
        
        //自动布局
         imageView.snp_makeConstraints(closure: { (make) -> Void in
            
            make.width.equalTo(self.contentView.snp_width)
            make.height.equalTo(self.contentView.snp_height)
            
         })
        beginBtn.snp_makeConstraints(closure: { (make) -> Void in
            make.bottom.equalTo(self.contentView.snp_bottom).multipliedBy(0.8)
            make.centerX.equalTo(self.contentView.snp_centerX)
            make.width.equalTo(200)
            make.height.equalTo(40)
            
        })
        
    }
    
// MARK:-------播放按钮动画
    private func startAnimation(){
        
        self.beginBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1.6, delay: 0.1, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
                self.beginBtn.transform = CGAffineTransformIdentity
            
                //执行动画时不能点击
                self.beginBtn.userInteractionEnabled = false
            }) { (isFinished) -> Void in
                self.beginBtn.userInteractionEnabled = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
